﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{
    public PlayerObject PlayerObject;
    public GridManager GridManager;
    public TerisController TerisController;
    [HideInInspector]
    public Vector2 PosBefore = new Vector2();
    public int HP = 1;
    public Canvas GameOverCanvas;
    public int Score = 0;
    public Text ScoreText;
    public int RoundsLeft = 20;
    public Text RoundText;
    public Text BossComeText;

    public AudioSource BGM1Audio, BGM2Audio, GameOverAudio;

    private LinkedList<EnemyObject> enemys = new LinkedList<EnemyObject>();
    // Start is called before the first frame update
    void Start()
    {
        ScoreText.text = Score.ToString();
        RoundText.text = RoundsLeft.ToString();
        //BGM1Audio.Play();
    }

    // Update is called once per frame
    [SerializeField]
    private int MoveFlag = 1;
    void Update()
    {
        if(GameOverCanvas.gameObject.activeInHierarchy) {
            if(Input.anyKeyDown)
                SceneManager.LoadScene("SampleScene");
            return;
        }
        if(MoveFlag == 0)
            return;
        if(Input.GetKeyDown(KeyCode.F)) {
            PosBefore = new Vector2((int)(GridManager.Player.PlayerObject.slot.GridPosition.x + 0.1f), 
                (int)(GridManager.Player.PlayerObject.slot.GridPosition.y + 0.1f));
            MoveFlag = 0;
            TerisController.StartSetTeris();
            return;
        }
        else if(Input.GetKeyDown(KeyCode.W)) {
            if(PlayerObject.Move(0, 1)) {
                PosBefore = new Vector2((int)(PlayerObject.slot.GridPosition.x + 0.1f),
                    (int)(PlayerObject.slot.GridPosition.y - 1 + 0.1f));
                MoveFlag = 0;
            }
        }
        else if(Input.GetKeyDown(KeyCode.A)) {
            if(PlayerObject.Move(-1, 0)) {
                PosBefore = new Vector2((int)(PlayerObject.slot.GridPosition.x + 1 + 0.1f),
                    (int)(PlayerObject.slot.GridPosition.y + 0.1f));
                MoveFlag = 0;
            }
        }
        else if(Input.GetKeyDown(KeyCode.S)) {
            if(PlayerObject.Move(0, -1)) {
                PosBefore = new Vector2((int)(PlayerObject.slot.GridPosition.x + 0.1f),
                    (int)(PlayerObject.slot.GridPosition.y + 1 + 0.1f));
                MoveFlag = 0;
            }
        }
        else if(Input.GetKeyDown(KeyCode.D)) {
            if(PlayerObject.Move(1, 0)) {
                PosBefore = new Vector2((int)(PlayerObject.slot.GridPosition.x - 1 + 0.1f),
                    (int)(PlayerObject.slot.GridPosition.y + 0.1f));
                MoveFlag = 0;
            }
        }
        if(MoveFlag == 0) {
            StartEnemyMove();
        }
    }

    public void StartEnemyMove() {
        StartCoroutine(StartEnemyMoveCoroutine(0.4f));
    }

    IEnumerator StartEnemyMoveCoroutine(float duration) {
        yield return new WaitForSeconds(duration);
        //Debug.Log(enemys.Count);
        StartCoroutine(EnemyMoveCoroutine(enemys.First, 0.1f));
    }

    IEnumerator EnemyMoveCoroutine(LinkedListNode<EnemyObject> node, float duration) {
        yield return new WaitForSeconds(duration);
        if(node == null) {
            MoveFlag = 1;
            yield break;
        }
        bool Moved = node.Value.MoveToPlayer();
        if(Moved)
            StartCoroutine(EnemyMoveCoroutine(node.Next, 0.3f));
        else
            StartCoroutine(EnemyMoveCoroutine(node.Next, 0f));
    }

    public void ListAdd(EnemyObject enemy) {
        enemys.AddLast(enemy);
    }

    public void ListRemove(EnemyObject enemy) {
        enemys.Remove(enemy);
    }

    public void AddTeris(int option = 1) {
        TerisController.AddTeris(option);
    }

    public void SetMoveable() {
        MoveFlag = 1;
    }

    public void AddScore(int dScore) {
        Score += dScore;
        ScoreText.text = Score.ToString();
    }

    public void DecreaseRound() {
        if(RoundsLeft == 0)
            return;
        RoundsLeft--;
        if(RoundsLeft == 0) {
            RoundText.gameObject.SetActive(false);
            BossComeText.text = "Choose 0 to summon boss!";
            BGM1Audio.Stop();
        }
        RoundText.text = RoundsLeft.ToString();
    }

    public void Boss1Down() {
        BossComeText.text = "Boss has already come!";
        BGM2Audio.Play();
    }

    public void Hurt(int damage) {
        HP -= damage;
        if(HP <= 0) {
            GameOver();
        }
    }

    public void GameOver() {
        if(PlayerObject != null)
            Destroy(PlayerObject.gameObject);
        BGM1Audio.Stop();
        BGM2Audio.Stop();
        GameOverAudio.Play();
        GameOverCanvas.gameObject.SetActive(true);
    }
}
